package com.utc.cards.table.view;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;

import com.utc.cards.Constants;
import com.utc.cards.table.jade.agent.HostAgentManager;
import com.utc.cards.table.jade.agent.gameAgent.IGameAgent;
import com.utc.cards.table.jade.agent.hostAgent.IHostAgent;
import com.utc.cards.table.jade.agent.rulesAgent.IRulesAgent;
import com.utc.cards.utils.Utils;

public abstract class AbstractTableGameActivity extends Activity implements ITableGameActivity
{
    protected static Logger log = LoggerFactory.getLogger(AbstractTableGameActivity.class);

    protected MyReceiver receiver;

    protected IRulesAgent rulesAgent;
    protected IGameAgent gameAgent;
    protected IHostAgent hostAgent;
    protected Point displayDimentions = new Point();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        displayDimentions = Utils.getDisplayWindowsDimention(this);

        // reception des intents
        registerReceivers();

        // récupération des agents
        loadAgents();

        setContentView(getLayout());
        onCreateSpecificView(savedInstanceState);
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        log.debug("onDestroy()... unregister receiver");
        unregisterReceiver(receiver);
    }

    public abstract void onCreateSpecificView(Bundle savedInstanceState);

    private class MyReceiver extends BroadcastReceiver
    {

        @Override
        public void onReceive(Context context, Intent intent)
        {
            String action = intent.getAction();
            log.info("Received intent " + action);

            if (action.equalsIgnoreCase(Constants.UPDATE_BOARD))
            {
                updateBoard();
            }
            else if (action.equalsIgnoreCase(Constants.UPDATE_SCORE))
            {
                updateScore();
            }
            else if (action.equalsIgnoreCase(Constants.END_OF_TURN))
            {
                launchWinnerAnimation();
            }
            else if (action.equalsIgnoreCase(Constants.DISPLAY_WINNER))
            {
                displayGameWinner();
            }
        }
    }

    private void registerReceivers()
    {
        receiver = new MyReceiver();

        IntentFilter filter = new IntentFilter();
        filter.addAction(Constants.END_OF_TURN);
        filter.addAction(Constants.UPDATE_BOARD);
        filter.addAction(Constants.UPDATE_SCORE);
        filter.addAction(Constants.DISPLAY_WINNER);

        registerReceiver(receiver, filter);
    }

    public abstract Activity getThis();

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(getMenu(), menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
        // case R.id.game_menu_host:
        // // Intent showSettings = new Intent(this, SettingsActivity.class);
        // // startActivity(showSettings);
        // return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }

    private void loadAgents()
    {
        hostAgent = HostAgentManager.instance().getAgent(this, Constants.CARDS_HOST_AGENT_NAME, IHostAgent.class);
        gameAgent = HostAgentManager.instance().getAgent(this, Constants.CARDS_HOST_GAME_AGENT_NAME, IGameAgent.class);
        rulesAgent = HostAgentManager.instance().getAgent(this, Constants.CARDS_HOST_RULES_AGENT_NAME, IRulesAgent.class);
        log.debug("onCreate() : agent chargés pour l'activity de JEU");
    }

    public abstract int getLayout();

    public abstract int getMenu();
}
